package com.example.compose

import android.os.Bundle
import android.text.TextUtils
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.Composable
import androidx.compose.Model
import androidx.compose.state
import androidx.compose.unaryPlus
import androidx.ui.core.*
import androidx.ui.foundation.VerticalScroller
import androidx.ui.graphics.Color
import androidx.ui.input.EditorStyle
import androidx.ui.layout.*
import androidx.ui.material.*
import androidx.ui.text.TextStyle
import androidx.ui.text.font.FontStyle
import androidx.ui.text.font.FontWeight
import androidx.ui.text.style.TextDecoration
import org.nfunk.jep.JEP
import java.lang.Exception

class MainActivity : AppCompatActivity() {

    var exp = ExprWithMap()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MaterialTheme {
                Greeting()
            }
        }
    }

    @Composable
    fun Greeting() {
        VerticalScroller {
            Padding(left = 16.dp, right = 16.dp) {
                Column {
                    val state = +state { "" }
                    FlexRow(
                        crossAxisAlignment = CrossAxisAlignment.Start
                    ) {
                        inflexible {
                            Container() {
                                Text {
                                    Span(
                                        text = "公式： ",
                                        style = TextStyle(
                                            color = Color(0xFFFF0000),
                                            fontSize = 18.sp,
                                            fontWeight = FontWeight.W200,
                                            fontStyle = FontStyle.Normal
                                        )
                                    )
                                }
                            }
                        }
                        expanded(1f) {
                            Column() {
                                TextField(
                                    value = state.value, onValueChange = { state.value = it }
                                )
                                Divider(
                                    height = 2.dp,
                                    color = if (TextUtils.isEmpty(state.value)) Color.Gray else Color.Blue
                                )
                            }

                        }
                    }
                    Button(
                        text = "生成",
                        onClick = {
                            exp.updateContent(state.value)
                        }
                    )
                }
//                Row {
//                    Text(
//                        "公式：",
//                        style = +themeTextStyle { body1 }
//                    )
//
//                    val state = +state { EditorModel("") }
//                    TextField(
//                        value = state.value,
//                        onValueChange = { state.value = it },
//                        editorStyle = EditorStyle(
//                            textStyle = TextStyle(
//                                fontSize = (50.sp)
//                            )
//                        )
//                    )
//
//                    Button(
//                        text = "生成",
//                        onClick = {
//                            exp.update(state.value.text)
//                        }
//                    )
//                }
                HeightSpacer(height = 20.dp)

//                repeat(exp.map?.size ?: 0) {
//                    for (key in exp.map?.keys!!) {
//                        Row() {
//                            Text(
//                                key,
//                                style = +themeTextStyle { body1 }
//                            )
//                            val state = +state { EditorModel("" + exp.map?.get(key)) }
//                            TextField(
//                                value = state.value,
//                                onValueChange = {
//                                    state.value = it
//                                    exp.map?.set(key, it.text.toDouble())
//                                },
//                                editorStyle = EditorStyle(
//                                    textStyle = TextStyle(
//                                        fontSize = (50.sp)
//                                    )
//                                )
//                            )
//                        }
//                    }
//                }

                Row {
                    Button(
                        text = "计算",
                        onClick = {
                            calc(exp)
                        }
                    )

                    Text(
                        "" + exp.res,
                        style = +themeTextStyle { body1 }
                    )
                }
            }
        }
    }
}

fun calc(exp: ExprWithMap) {
    val jep = JEP()
//    var expression = "(数量 * 0.99 - 代理费用 - 促销费用) * 0.3"
    var expression = exp.expr
    var list = mutableListOf<String>()
    expression = expression.replace(" ", "")
    list.addAll(expression.split(Regex("[\\(\\)\\+\\-\\*\\/]")))
    var res = list.filter { !TextUtils.isEmpty(it) }
    var map = LinkedHashMap<String, Double>()
    for (str in res) {
        try {
            var d = str.toDouble()
        } catch (e: Exception) {
            map.put(str, 0.0)
        }
    }
    for (key in map.keys) {
//        System.out.print("" + map[key])
        jep.addVariable(key, exp.map?.get(key))
    }

    jep.parseExpression(expression)
    exp.res = jep.value
}


@Model
class ExprWithMap() {
    var res = 0.0
    var expr = ""
    var map: LinkedHashMap<String, Double>? = null
    fun updateContent(str: String?) {
        if (TextUtils.isEmpty(str)) {
            return
        }
        expr = str!!
        var list = mutableListOf<String>()
        expr = expr.replace(" ", "")
        list.addAll(expr.split(Regex("[\\(\\)\\+\\-\\*\\/]")))
        var res = list.filter { !TextUtils.isEmpty(it) }
        map = LinkedHashMap<String, Double>()
        for (str in res) {
            try {
                var d = str.toDouble()
            } catch (e: Exception) {
                map?.put(str, 0.0)
            }
        }
    }
}